﻿Imports System.Data
Imports System.IO
Imports System.IO.Compression
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary

Public Module DCM_ObjectToByte '"数据序列化压缩"
    ''' <summary>
    ''' 解压数据
    ''' </summary>
    ''' <param name="data"></param>
    ''' <returns></returns>
    Public Function Decompress(data As Byte()) As Byte()
        If IsNothing(data) Then Return Nothing
        Dim bData As Byte()
        Dim ms As New MemoryStream()
        ms.Write(data, 0, data.Length)
        ms.Position = 0
        Dim stream As New GZipStream(ms, CompressionMode.Decompress, True)
        Dim buffer As Byte() = New Byte(1023) {}
        Dim temp As New MemoryStream()
        Dim read As Integer = stream.Read(buffer, 0, buffer.Length)
        While read > 0
            temp.Write(buffer, 0, read)
            read = stream.Read(buffer, 0, buffer.Length)
        End While
        '必须把stream流关闭才能返回ms流数据,不然数据会不完整
        stream.Close()
        stream.Dispose()
        ms.Close()
        ms.Dispose()
        bData = temp.ToArray()
        temp.Close()
        temp.Dispose()
        Return bData
    End Function

    ''' <summary>
    ''' 压缩数据
    ''' </summary>
    ''' <param name="data"></param>
    ''' <returns></returns>
    Public Function Compress(data As Byte()) As Byte()
        Dim bData As Byte()
        Dim ms As New MemoryStream()
        Dim stream As New GZipStream(ms, CompressionMode.Compress, True)
        stream.Write(data, 0, data.Length)
        stream.Close()
        stream.Dispose()
        '必须把stream流关闭才能返回ms流数据,不然数据会不完整
        '并且解压缩方法stream.Read(buffer, 0, buffer.Length)时会返回0
        bData = ms.ToArray()
        ms.Close()
        ms.Dispose()
        Return bData
    End Function
    ''' <summary> 
    ''' 将object格式化成字节数组byte[]
    ''' </summary>
    ''' <param name="dsOriginal">object对象</param>
    ''' <returns>字节数组</returns>
    Public Function GetBinaryFormatData(dsOriginal As Object) As Byte()
        Dim binaryDataResult As Byte() = Nothing
        Dim memStream As New MemoryStream()
        Dim brFormatter As IFormatter = New BinaryFormatter()
        brFormatter.Serialize(memStream, dsOriginal)
        binaryDataResult = memStream.ToArray()
        memStream.Close()
        memStream.Dispose()
        Return binaryDataResult
    End Function
    ''' <summary>
    ''' 将objec格式化成字节数组byte[]，并压缩
    ''' </summary>
    ''' <param name="dsOriginal">object对象</param>
    ''' <returns>字节数组</returns>
    Public Function GetBinaryFormatDataCompress(dsOriginal As Object) As Byte()
        Dim binaryDataResult As Byte() = Nothing
        Dim memStream As New MemoryStream()
        Dim brFormatter As IFormatter = New BinaryFormatter()
        If dsOriginal.GetType Is GetType(DataTable) Then dsOriginal.RemotingFormat = SerializationFormat.Binary
        brFormatter.Serialize(memStream, dsOriginal)
        binaryDataResult = memStream.ToArray()
        memStream.Close()
        memStream.Dispose()
        Return Compress(binaryDataResult)
    End Function

    ''' <summary>
    ''' 将字节数组反序列化成object对象
    ''' </summary>
    ''' <param name="binaryData">字节数组</param>
    ''' <returns>object对象</returns>
    Public Function RetrieveObject(binaryData As Byte()) As Object
        Dim memStream As New MemoryStream(binaryData)
        Dim brFormatter As IFormatter = New BinaryFormatter()
        Dim obj As [Object] = brFormatter.Deserialize(memStream)
        Return obj
    End Function
    ''' <summary>
    ''' 将字节数组反解压后序列化成DataSet对象
    ''' </summary>
    ''' <param name="binaryData">字节数组</param>
    ''' <returns>DataSet对象</returns>
    Public Function RetrieveObjectDecompress(binaryData As Byte()) As Object
        If IsNothing(binaryData) Then Return Nothing
        Dim memStream As New MemoryStream(Decompress(binaryData))
        Dim brFormatter As IFormatter = New BinaryFormatter()
        Dim obj As [Object] = brFormatter.Deserialize(memStream)
        Return obj
    End Function
End Module